import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:gw_github_flutter/common/route_name.dart';
import 'package:gw_github_flutter/tools/gwEasyRefrashManager.dart';
import 'package:gw_github_flutter/tools/gwGetManager.dart';

class SliverRouteAppBar extends StatefulWidget {
  SliverRouteAppBar({Key? key}) : super(key: key);

  @override
  State<SliverRouteAppBar> createState() => _SliverRouteAppBarState();
}

class _SliverRouteAppBarState extends State<SliverRouteAppBar> {
  var _tabs = <String>[
    "Tab 1",
    "Tab 2",
    "Tab 3",
  ];
  var _itemCount = 10;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        child: DefaultTabController(
          length: _tabs.length, // This is the number of tabs.
          child: NestedScrollView(
            headerSliverBuilder:
                (BuildContext context, bool innerBoxIsScrolled) {
              // These are the slivers that show up in the "outer" scroll view.
              return <Widget>[
                SliverOverlapAbsorber(
                  handle:
                      NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                  sliver: SliverAppBar(
                    backgroundColor: Colors.blue,
                    leading: new IconButton(
                      icon: Icon(Icons.arrow_back),
                      onPressed: () => GwGetRouteManager.pop(),
                    ),
                    title: const Text(RouteName.sliverRoute_appBar),
                    centerTitle: true,
                    pinned: true,
                    // 设置滑动导航栏的显隐
                    floating: false,
                    snap: false,
                    primary: true,
                    expandedHeight: 230.0,
                    // 阴影大小
                    elevation: 10,
                    //是否显示阴影，直接取值innerBoxIsScrolled，展开不显示阴影，合并后会显示
                    forceElevated: innerBoxIsScrolled,

                    actions: <Widget>[
                      new IconButton(
                        icon: Icon(Icons.more_horiz),
                        onPressed: () {
                          print("更多");
                        },
                      ),
                    ],

                    flexibleSpace: new FlexibleSpaceBar(
                      background:
                          Image.asset("imgs/fengjing.png", fit: BoxFit.fill),
                    ),

                    bottom: TabBar(
                      indicatorColor: Colors.red,
                      tabs:
                          _tabs.map((String name) => Tab(text: name)).toList(),
                    ),
                  ),
                ),
              ];
            },
            body: TabBarView(
              children: _tabs.map((String name) {
                //SafeArea 适配刘海屏的一个widget
                return SafeArea(
                    top: false,
                    bottom: false,
                    child: Builder(
                      builder: (BuildContext context) {
                        return EasyRefresh.builder(
                          key: Key(name),
                          footer: GwEasyRefrashManager.setRefrashFooter(
                              mainAxisAlignment: MainAxisAlignment.end),
                          onLoad: () async {
                            await Future.delayed(Duration(seconds: 2), () {
                              if (mounted) {
                                setState(() {
                                  _itemCount += 10;
                                });
                              }
                            });
                          },
                          onRefresh: () async {
                            await Future.delayed(Duration(seconds: 2), () {
                              if (mounted) {
                                setState(() {
                                  _itemCount = 10;
                                });
                              }
                            });
                          },
                          childBuilder: (context, physics) {
                            return CustomScrollView(
                              key: Key(name),
                              physics: physics,
                              slivers: <Widget>[
                                SliverOverlapInjector(
                                  handle: NestedScrollView
                                      .sliverOverlapAbsorberHandleFor(context),
                                ),
                                const HeaderLocator.sliver(
                                  clearExtent: true,
                                ),
                                SliverPadding(
                                  padding: const EdgeInsets.all(10.0),
                                  sliver: SliverFixedExtentList(
                                    key: Key(name),
                                    itemExtent: 50.0, //item高度或宽度，取决于滑动方向
                                    delegate: SliverChildBuilderDelegate(
                                      (BuildContext context, int index) {
                                        return ListTile(
                                          title: Text('Item $index'),
                                        );
                                      },
                                      childCount: _itemCount,
                                    ),
                                  ),
                                ),
                                const FooterLocator.sliver(
                                  clearExtent: true,
                                ),
                              ],
                            );
                          },
                        );
                      },
                    ));
              }).toList(),
            ),
          ),
        ),
      ),
    );
  }
}
